|
|
|
|
|
|
|
REFERENCES Supplier(SupplierID))
CREATE TABLE DeptSupplier
(DeptID TEXT(5),
SupplierID TEXT(10),
CONSTRAINT PKDeptSpl PRIMARY KEY (DeptID,SupplierID),
CONSTRAINT FKDptSplDept FOREIGN KEY (DeptID)
REFERENCES Departments(DeptID),
CONSTRAINT FKDptSplSupplier FOREIGN KEY (SupplierID)
REFERENCES Supplier(SupplierID)) |
|
|
|
|
|
|
|
|
Notice that, in these two tables, the CONSTRAINT definitions are longer than the field definitions. This is common when you begin to use the power database integrity aspects of SQL databases. |
|
|
|
|
|
|
|
|
Finally, you need a single table that expresses the Equipment-Supplier-Department relationship. This table shows which department has which equipment supplied by which supplier. More importantly, you can build this final table with tight constraints that enforce all these business rules. Both the Department-Supplier relationship and the Equipment-Supplier relationship are validated before the record is saved to the database. This is a powerful data validation toolall without writing any Visual Basic code! Table 16.7 and the SQL DDL statement in Listing 16.10 show how this table can be constructed. See Figure 16.9 to review the results of executing these statements. |
|
|
|
|
| Table 16.7. The Department-Equipment-Supplier data table. | | DeptID | | EquipID | | SupplierID |
|
|
|
|
|
|
Listing 16.10. Creating the DeptEqpSuplr table. |
|
|
|
|
|
|
|
|
CREATE TABLE DeptEqpSuplr
(DeptID TEXT(5),
EquipID TEXT(10),
SupplierID TEXT(10),
CONSTRAINT PFDeptEq PRIMARY KEY (DeptID, EquipID),
CONSTRAINT FKEqSupl FOREIGN KEY (EquipID,SupplierID)
REFERENCES EquipSupplier(EquipID,SupplierID),
CONSTRAINT FKDeptSupl FOREIGN KEY (DeptID,SupplierID)
REFERENCES DeptSupplier(DeptID,SupplierID)) |
|
|
|
|
|
|
|
|
The Microsoft Access database NORMDAT6.MDB contains a set of live data for the tables described in this section. Use Visdata to open the database and review the table structure. Try adding or deleting records in ways that would break integrity rules. Notice that none of the last three |
|
|
|
|
|